package com.sangeng.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.AddCategoryDto;
import com.sangeng.domain.dto.UpdateCategoryDto;
import com.sangeng.domain.vo.CategoryVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.CategoryMapper;
import com.sangeng.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping("/list")
    public ResponseResult<PageVo> getCategoryList(Integer pageNum,Integer pageSize) {
        return categoryService.getCategoryList(pageNum, pageSize);
    }

    @GetMapping("/listAllCategory")
    public ResponseResult<List<CategoryVo>> listAllCategory(){
        return categoryService.getAllCategoryList();
    }

    @PostMapping
    public ResponseResult<String> addCategory(@RequestBody @Validated AddCategoryDto categoryDto) {
        return categoryService.addCategory(categoryDto);
    }

    //修改分类 先获取数据 然后修改的时候自动填充到那个框上
    @GetMapping("/{id}")
    public ResponseResult<CategoryVo>  getCategory(@PathVariable Long id) {
        return categoryService.getCategoryById(id);
    }
    @PutMapping
    public ResponseResult<String> updateCategory(@RequestBody UpdateCategoryDto categoryDto) {
        return categoryService.updateCategory(categoryDto);
    }

    //多选删除
    @DeleteMapping("/{idList}")
    public ResponseResult<String> deleteCategory(@PathVariable List<Long> idList) {
        return categoryService.deleteCategoryById(idList);
    }

    // TODO 导出分类excel表功能 参考easyExcel中的示例
    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermissions('content:category:export')")
    public void export(HttpServletResponse response) {
        return ;
    }
}
